#define  _CRT_SECURE_NO_WARNINGS 1

#include"stack.h"

void STInit(ST* ps)
{
	assert(ps);
	
	ps->a = (STDataType*)malloc(sizeof(STDataType)*4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}

	ps->cpacity = 4;
	ps->top = 0;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->top = 0;
	ps->cpacity = 0;
}

bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);

	if (ps->cpacity == ps->top)
	{
		STDataType* tmp = realloc(ps->a, sizeof(STDataType) * ps->cpacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->cpacity *= 2;
	}

	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	ps->top--;
}

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	return ps->a[ps->top - 1];
}

int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

